<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="接口类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="apiType">
              <j-dict-select-tag type="radio" v-model="model.apiType" dictCode="data_type_api" placeholder="请选择接口类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="model.apiType==='1'">
            <a-form-model-item label="需要Token" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="hasToken">
              <j-dict-select-tag type="radio" v-model="model.hasToken" dictCode="yn" placeholder="请选择是否需要Token" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="model.apiType==='1'">
            <a-form-model-item label="数据类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag type="radio" v-model="model.type" dictCode="data_api_type" placeholder="请选择数据类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="接口Code" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="请输入接口Code"  ></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="接口描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" rows="4" placeholder="请输入接口描述" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="接口名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入接口名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="model.apiType==='2'">
            <a-form-model-item label="接口地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sqlContent">
              <a-input v-model="model.sqlContent" placeholder="请输入接口地址"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="model.apiType==='2'">
            <a-form-model-item label="请求方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="methodType">
              <a-input v-model="model.methodType" placeholder="请输入请求方式"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="model.apiType==='1'">
            <a-form-model-item label="数据源" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dataSource">
              <j-dict-select-tag type="list" v-model="model.dataSource" dictCode="sys_data_source,name,code" placeholder="请选择数据源" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24"  v-if="model.apiType==='1'">
            <a-form-model-item label="." :labelCol="{xs: { span: 24 },sm: { span: 3 }}" :wrapperCol="{xs: { span: 24 },sm: { span: 20 }}" prop="">
              <a-alert
                message=" 您可以键入“”作为一个参数，这里abc是参数的名称。例如：
                          select * from table where id = ${abc}。
                          select * from table where id like concat('%',${abc},'%')。(mysql模糊查询)<br>
                          select * from table where id like '%'||${abc}||'%'。(oracle模糊查询)<br>
                          select * from table where id like '%'+${abc}+'%'。(sqlServer模糊查询)<br>
                          注：参数只支持动态报表，popup暂不支持"
                type="warning"
                closable
              />
            </a-form-model-item>
          </a-col>
         <!-- <a-col :span="24">
            <a-form-model-item label="查询SQL" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sqlContent">
              <a-textarea v-model="model.sqlContent" rows="5" placeholder="请输入查询SQL" />
            </a-form-model-item>
          </a-col>-->
          <a-col :span="24" v-if="model.apiType==='1'">
            <a-form-model-item label="查询SQL" :labelCol="{xs: { span: 24 },sm: { span: 3 }}" :wrapperCol="{xs: { span: 24 },sm: { span: 20 }}" prop="sqlContent">
              <j-code-editor ref="codeEditor"
                language="sql"
                placeholder="请输入SQL语句"
                :lineNumbers="false"
                :fullScreen="true"
                style="min-height: 100px"/>
            </a-form-model-item>
          </a-col>
         <!-- <a-col :span="12">
            <a-form-model-item label="数据状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="radio" v-model="model.status" dictCode="status" placeholder="请选择数据状态" />
            </a-form-model-item>
          </a-col>-->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import JCodeEditor from '@/components/jeecg/JCodeEditor'
  export default {
    name: 'SysDataApiForm',
    components: {
      JCodeEditor,
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          type:"1",
          hasToken:"0",
          apiType:"1",
          methodType:'POST',
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           hasToken: [
              { required: true, message: '请输入是否需要token!'},
           ],
          apiType: [
              { required: true, message: '请选择接口类型'},
           ],
           type: [
              { required: true, message: '请选择数据类型!'},
           ],
           code: [
              { required: true, message: '请输入接口Code!'},
           ],
           name: [
              { required: true, message: '请输入接口名称!'},
           ],
           dataSource: [
              { required: true, message: '请输入数据源!'},
           ],
          methodType: [
              { required: true, message: '请输入请求方式!'},
           ],
          /* sqlContent: [
              { required: true, message: '请输入查询SQL!'},
           ],*/
           status: [
              { required: true, message: '请输入数据状态!'},
           ],
        },
        url: {
          add: "/sysDataApi/add",
          edit: "/sysDataApi/edit",
          queryById: "/sysDataApi/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
        //console.log(this.model)
        this.$refs.codeEditor.coder.setValue(this.model.sqlContent);

      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            if(this.model.apiType==='1'){
              let sqlContent = this.$refs.codeEditor.code;
              if(!sqlContent){
                that.$message.warning('请输入SQL语句');
                that.confirmLoading = false;
                return false;
              }
              this.model.sqlContent = sqlContent;
            }


            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>